package com.search.analysis;

import com.search.entity.SearchContentEntity;
import com.search.utils.ExecutorUtils;
import javafx.scene.layout.Pane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.LinkedBlockingQueue;

@Component
public class FactoryAnalysis {
   public static LinkedBlockingQueue<Pane> queue = new LinkedBlockingQueue();

    @Autowired
    private List<Analysis> analyseslist;

    public LinkedBlockingQueue<Pane> searchBean(String keyWord) {

        for (Analysis analyses : analyseslist) {
            ExecutorUtils.create("Analysis", () -> {
                List<Pane> fail = isFail(keyWord, analyses);
                //向队列中添加数据
                if (fail != null) {
                    queue.addAll(fail);
                }
            });
        }
        return queue;
    }


    //判空
    private List<Pane> isFail(String keyWord, Analysis analyses) {
        //耗时最高的是获取数据getData平均都是2s+ ,其他方法都是很快的10~100ms
        String data = analyses.getData(keyWord);
        if (data == null) {
            return null;
        }
        List<Map<String, Object>> extract = analyses.extract(data);
        if (extract == null) {
            return null;
        }
        List<SearchContentEntity> rinse = analyses.rinse(extract);
        if (rinse == null) {
            return null;
        }
        List<SearchContentEntity> process = analyses.process(rinse);
        if (process == null) {
            return null;
        }
        List<Pane> vBox = analyses.toVBox(process);
        if (vBox == null) {
            return null;
        }
        return vBox;
    }


}
